package com.xmwzw.catpu;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.xmwzw.catpu.bean.SoftBean;
import com.xmwzw.catpu.databinding.FragmentTypeListBinding;
import com.xmwzw.catpu.db.FlowManagerUtil;
import com.xmwzw.catpu.db.SoftModel;
import com.xmwzw.catpu.db.SoftModel_Table;
import com.xmwzw.catpu.http.BaseRequest;
import com.xmwzw.catpu.http.listener.ItemListener;
import com.xmwzw.catpu.item.SoftItem;
import com.xmwzw.catpu.util.CommonUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : wangzw
 * @date : 20-4-13下午6:00
 * @desc :
 */
public class TypeListFragment extends BaseFragment {
    private FastItemAdapter fastItemAdapter = new FastItemAdapter();
    private List<SoftItem> items = new ArrayList<>();
    private List<SoftModel> models = new ArrayList<>();

    private FragmentTypeListBinding listBinding;

    private int loadCount = 0;


    private int type;

    @Override
    public int getContentLayout() {
        return R.layout.fragment_type_list;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getInt("type", 0);
        }
    }

    @Override
    public void findViews(View view) {
        listBinding = getBaseViewBinding();
        listBinding.rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        fillData();
    }

    private void fillData() {
        loadCount++;
        items.clear();
        List<SoftModel> softModels = SQLite.select().from(SoftModel.class).where(SoftModel_Table.type_id.eq(String.valueOf(type)))
                .queryList();

        if (softModels.isEmpty() && loadCount <= 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    fillData();
                }
            },2000);
            fillData();
            return;
        }
        for (int i = 0; i < softModels.size(); i++) {
            SoftModel softModel = softModels.get(i);
            SoftBean softBean = CommonUtil.getSoftBean(softModel, String.valueOf((i + 1)));
            items.add(new SoftItem().fillData(false, softBean));
        }

        fastItemAdapter.set(items);
        listBinding.rvList.setAdapter(fastItemAdapter);


    }


}
